package com.lin.videospringboot.service;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.videospringboot.been.Post;
import com.lin.videospringboot.mapper.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl {

    @Autowired
    PostMapper postMapper;

    public List<Post> queryAllList() {
        return postMapper.selectList(null);
    }

    public int addPost(Post post) {
        return postMapper.insert(post);
    }



    public List<Post> pageQuery(int start,int len) {
        Page<Post> page = new LambdaQueryChainWrapper<>(postMapper).page(new Page<>(start,len));
        return page.getRecords();
    }

    public Page<Map<String,Object>> pageQueryWithUserInfo(int start, int len) {
        System.out.println(start/len);
        Page<Map<String,Object>> page = new Page<> (start/len+1,len);

        return page.setRecords(postMapper.getPostWithUserInfo(page));
    }

    public int queryNum() {
        return postMapper.selectCount(null);
    }


    public Post getPostById(int postId) {
        return postMapper.selectById(postId);
    }

    public int updatePost(Post post) {
        return postMapper.updateById(post);
    }
}
